package com.evon.yardmanagement.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.annotations.ParamDef;

@Entity
@Table(name = "USER")
//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@FilterDef(name="YARD_ROLE", parameters= {@ParamDef(name = "YARD_ID", type = "int")})
@AttributeOverride(name = "id", column = @Column(name = "USER_ID"))
public class User extends BaseModel implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column(name = "USER_NAME")
	private String userName;

	@Column(name = "FIRST_NAME")
	private String firstName;

	@Column(name = "MIDDLE_NAME")
	private String middleName;

	@Column(name = "LAST_NAME")
	private String lastName;

	// TODO : This should be binary or encrypted
	@Column(name = "Passwd")
	private String passwd;

	@ManyToOne
	@JoinColumn(name="COMPANY_ID",referencedColumnName="COMPANY_ID")
	
	private Vendor vendor;
	
	public Vendor getVendor() {
		return vendor;
	}

	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}

	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	private Set<Role> roles;
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID", unique = true) }, inverseJoinColumns = { @JoinColumn(name = "YARD_ID") })
	@FilterJoinTable(name="YARD_ROLE", condition=":YARD_ID = YARD_ID")
	private Set<Yard> accessibleYards;
	

	public Set<Yard> getAccessibleYards() {
		return accessibleYards;
	}

	public void setAccessibleYards(Set<Yard> accessibleYards) {
		this.accessibleYards = accessibleYards;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}


}
